﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CDB.LinqModel.Data;
using CDB.LinqModel;
using CDB.LinqData;

public partial class back_AddNewsLetter : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    NewsLetter newsLetter;
    #endregion

    #region Page Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["admin"] != null)
            {
                if (Request.QueryString["Id"] != null)
                {
                    fillNewsLetter(Request.QueryString["Id"].ToString());
                }
            }
            else
                Response.Redirect("login.aspx");
        }
    }
    #endregion

    #region Methods
    private void fillNewsLetter(string newsLetterId)
    {
        newsLetter = new NewsLetter(_dataObject);
        newsLetter.Id = Convert.ToInt16(newsLetterId);
        newsLetter.GetNewsLetterById();

        if (newsLetter.Id != 0)
        {
            txtSubject.Text = newsLetter.Subject;
            txtPageContent.InnerHtml = newsLetter.Content;
        }
    }
    #endregion

    #region Button Events
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        newsLetter = new NewsLetter(_dataObject);

        if (Request.QueryString["Id"] != null)
        {
            newsLetter.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
            newsLetter.Subject = txtSubject.Text.ToString().Trim();
            newsLetter.Content = txtPageContent.InnerHtml.ToString().Trim();

            newsLetter.UpdateById();

            divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Newsletter updated successfully!";
            divMessage.Attributes.Add("class", "alert alert-success");
        }
        else
        {
            newsLetter.Subject = txtSubject.Text.ToString().Trim();
            newsLetter.GetNewsLetterByTitle();

            if (newsLetter.Id != 0)
            {
                divMessage.InnerHtml = "<strong>Oh snap!</strong>&nbsp;Newsletter with the same name already exists!";
                divMessage.Attributes.Add("class", "alert alert-error");
            }
            else
            {
                newsLetter.Subject = txtSubject.Text.ToString().Trim();
                newsLetter.Content = txtPageContent.InnerHtml.ToString().Trim();

                newsLetter.Create();

                divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Newsletter added successfully!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
        }
    }
    #endregion
}
